package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.SecurityAdvisor
import cn.exrick.xboot.base.entity.SecurityAdvisorPublish
import cn.exrick.xboot.base.mapper.SecurityAdvisorPublishMapper
import cn.exrick.xboot.base.service.IBusinessService
import cn.exrick.xboot.base.service.ISecurityAdvisorPublishService
import cn.exrick.xboot.base.service.ISecurityAdvisorService
import cn.exrick.xboot.base.service.ISecurityAdvisorServiceEnterpriseService
import cn.exrick.xboot.base.vo.SecurityAdvisorPublishVo
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import cn.exrick.xboot.core.service.UserService
import cn.hutool.core.date.DateUnit
import cn.hutool.core.date.DateUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 安全顾问发布接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class ISecurityAdvisorPublishServiceImpl : ServiceImpl<SecurityAdvisorPublishMapper?, SecurityAdvisorPublish?>(), ISecurityAdvisorPublishService {

    @Autowired
    private var securityUtil: SecurityUtil? = null
    @Autowired
    private var userService: UserService? = null
    @Autowired
    private var iSecurityAdvisorService: ISecurityAdvisorService? = null
    @Autowired
    private var iSecurityAdvisorServiceEnterpriseService: ISecurityAdvisorServiceEnterpriseService? = null
    @Autowired
    private lateinit var iBusinessService: IBusinessService

    override fun createOrUpdateSecurityAdvisorPublish(securityAdvisorPublish: SecurityAdvisorPublish): Result<Any?>? {

        return try {
            saveOrUpdate(securityAdvisorPublish)
            ResultUtil.success<Any>("发布成功")
        }catch (e: Exception) {
            ResultUtil.error(e.message)
        }
    }

    override fun getSecurityAdvisorPublishDetail(id: String?): Result<SecurityAdvisorPublishVo?>? {


        securityUtil!!.currUser.birth ?: return ResultUtil.error("请完善生日信息")

        val userId = if (ToolUtil.isNotEmpty(id)) iSecurityAdvisorService!!.getById(id)?.userId else securityUtil!!.currUser.id
        return getOne(KtQueryWrapper(SecurityAdvisorPublish::class.java).eq(SecurityAdvisorPublish::createBy, userId)
                .orderByDesc(SecurityAdvisorPublish::createTime).last("limit 1"))?.run {

            ResultUtil.data(getSecurityAdvisorDetailVo(this), "获取发布详情成功！")
        } ?: ResultUtil.error(201, "暂无数据")
    }

    override fun getSecurityAdvisorPublishList(pageVo: PageVo): Result<MutableMap<String, Any?>>? {

        return ToolUtil.setListToNul(list(KtQueryWrapper(SecurityAdvisorPublish::class.java).orderByDesc(SecurityAdvisorPublish::createTime)))?.run {

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { getSecurityAdvisorDetailVo(it) })
            ResultUtil.data(result, "获取成功")
        } ?: ResultUtil.error(201, "暂无数据！")
    }

    private fun getSecurityAdvisorDetailVo(securityAdvisorPublish: SecurityAdvisorPublish?): SecurityAdvisorPublishVo? {

       return securityAdvisorPublish?.run {

           SecurityAdvisorPublishVo().apply {

               //个人信息
               userService!!.get(createBy).let {

                   ToolUtil.copyProperties(it, this)
                   it.birth?.run {
                       age = DateUtil.ageOfNow(this)
                   }
               }

               //安全顾问信息
               iSecurityAdvisorService!!.getOne(KtQueryWrapper(SecurityAdvisor::class.java).eq(SecurityAdvisor::userId, createBy))
                       .let { businessType = it?.businessIds?.split(",")?.mapNotNull { iBusinessService.getBusinessNameById(it) }?.toMutableList() }

               //发布信息
               serviceEnterpriseTypeList = serviceEnterpriseType!!.split(",").toMutableList()

               ToolUtil.copyProperties(this@run, this)

               //服务过的公司
               securityAdvisorServiceEnterpriseVoList = iSecurityAdvisorServiceEnterpriseService!!.getSecurityAdvisorServiceEnterpriseVoList(createBy)

           }
       }
    }

}
